package com.evil.application.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.evil.account.api.RemoteUserService;
import com.evil.application.api.RemoteApplicationOrderMessageService;
import com.evil.application.mapper.ApplicationOrderMapper;
import com.evil.application.mapper.ApplicationOrderMessageMapper;
import com.evil.application.pojo.dto.order.message.AddOrderMessageReqDTO;
import com.evil.application.pojo.dto.order.message.OrderMessageReqDTO;
import com.evil.application.pojo.dto.order.message.OrderMessageRespDTO;
import com.evil.application.pojo.entity.ApplicationOrderMessage;
import com.evil.application.service.ApplicationOrderMessageService;
import com.evil.common.account.dto.user.UserBaseRespDTO;
import com.evil.common.core.dto.FindByIdsReqDTO;
import com.evil.common.core.entity.QueryModel;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.core.util.StreamUtil;
import com.evil.common.file.dto.FileRespDTO;
import com.evil.file.api.RemoteFileService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * 应用订单留言表
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@RestController
@AllArgsConstructor
public class ApplicationOrderMessageServiceImpl extends ServiceImpl<ApplicationOrderMessageMapper, ApplicationOrderMessage> implements ApplicationOrderMessageService, RemoteApplicationOrderMessageService {

    private final ApplicationOrderMapper applicationOrderMapper;

    private final RemoteFileService remoteFileService;

    private final RemoteUserService remoteUserService;

    private final ApplicationLogServiceImpl applicationLogService;

    /**
     * 订单留言
     *
     * @param queryModel queryModel
     * @return List
     */
    @Override
    public PageInfo<OrderMessageRespDTO> orderMessage(QueryModel<OrderMessageReqDTO> queryModel) {
        PageInfo<ApplicationOrderMessage> pageInfo = this.baseMapper.orderMessagePage(queryModel);

        Set<Long> fileIds = new HashSet<>();
        Set<Long> creatorIds = new HashSet<>();
        pageInfo.getList().forEach(e -> {
            Optional.ofNullable(e.getFileId()).ifPresent(fileIds::add);
            creatorIds.add(e.getCreatorId());
        });

        Map<Long, FileRespDTO> fileMap = remoteFileService.findBaseMapByFileIds_Default(fileIds);
        Map<Long, UserBaseRespDTO> creatorMap = remoteUserService.findBaseMapByIds_Default(FindByIdsReqDTO.unFilter(creatorIds));

        Page<OrderMessageRespDTO> page = new Page<>(pageInfo.getPageNum(), pageInfo.getPageSize());
        page.setTotal(pageInfo.getTotal());
        page.addAll(StreamUtil.transListT(pageInfo.getList(), e -> {
            OrderMessageRespDTO orderMessageRespDTO = new OrderMessageRespDTO(e);
            if (fileMap.containsKey(e.getFileId())) {
                orderMessageRespDTO.setFile(fileMap.get(e.getFileId()));
            }
            if (creatorMap.containsKey(e.getCreatorId())) {
                orderMessageRespDTO.setCreator(creatorMap.get(e.getCreatorId()));
            }
            return orderMessageRespDTO;
        }));
        return page.toPageInfo();
    }

    /**
     * 新增订单留言
     *
     * @param addOrderMessageReqDTO addOrderMessageReqDTO
     */
    @Override
    public void addOrderMessage(AddOrderMessageReqDTO addOrderMessageReqDTO) {
        Long orderId = addOrderMessageReqDTO.getOrderId();
        String orderMessage = addOrderMessageReqDTO.getOrderMessage();
        String fileCode = addOrderMessageReqDTO.getFileCode();

        applicationOrderMapper.findById(orderId);

        ApplicationOrderMessage applicationOrderMessage = new ApplicationOrderMessage();
        applicationOrderMessage.setOrderId(orderId);
        applicationOrderMessage.setOrderMessage(orderMessage);
        if (StringUtils.isNotBlank(fileCode)) {
            FileRespDTO file = remoteFileService.findBaseByFileCode(fileCode);
            applicationOrderMessage.setFileId(file.getFileId());
        }
        applicationOrderMessage.setIsDeleted(SwitchEnum.NO.getId());

        applicationLogService.saveAndCheck(this.baseMapper, ApplicationOrderMessage::getOrderMessageId, applicationOrderMessage);
    }
}
